const jwt = require("koa-jwt")

const auth = jwt({
  secret: process.env.JWT_SECRET
})

const checkSelf = async (ctx, next) => {
  const uid = ctx.params.uid || ctx.request.body.uid
  if (!uid || !ctx.state.user._id || uid !== ctx.state.user._id)
    return ctx.throw(401)
  await next()
}

module.exports = {
  auth,
  checkSelf
}
